package system

import (
	"encoding/json"
	"sensitives/service/dic"
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
)

type gallery struct{}

type gallerycontent struct {
	Id          string
	Url         string
	Description string
}

var Gallery gallery

type gsensitive struct {
	Index    int           `json:"index"`
	IsArr    int           `json:"isArr"`
	Key      string        `json:"key"`
	NewIndex []interface{} `json:"newIndex"`
	Word     []string      `json:"word"`
	Level    uint8         `json:"level"`
	Type     string        `json:"type"`
}

var Gsensitive []gsensitive

// 只检测图集的内容字段
func (g *gallery) Check(context *gin.Context) {
	siteid, _ := strconv.Atoi(context.Param("siteid"))
	type Params struct {
		Title       string `form:"title"`
		Gallery     string `form:"gallery"`
		Description string `form:"description"`
		Keyword     string `form:"keyword"`
	}
	var params Params
	err := context.Bind(&params)
	Gsensitive = make([]gsensitive, 0)
	type OutPut struct {
		Code bool         `json:"code"`
		Data []gsensitive `json:"data"`
	}
	if err != nil {
		output := OutPut{Code: false, Data: Gsensitive}
		stive_sys, _ := json.Marshal(output)
		context.Writer.Write(stive_sys)
		return
	}
	site_sys_dic := dic.CloudMap[siteid].Sys
	if params.Title != "" {
		g.gfilter(params.Title, "title", &site_sys_dic)
	}
	if params.Gallery != "" {
		var gjson []gallerycontent
		err := json.Unmarshal([]byte(params.Gallery), &gjson)
		if err == nil {
			for i, gj := range gjson {
				g.gdesfilter(gj.Description, "gallery", i, &site_sys_dic)
			}
		}
	}
	if params.Description != "" {
		g.gfilter(params.Description, "description", &site_sys_dic)
	}
	if params.Keyword != "" {
		g.gfilter(params.Keyword, "keyword", &site_sys_dic)
	}
	output := OutPut{Code: true, Data: Gsensitive}
	stive_sys, _ := json.Marshal(output)
	context.Writer.Write(stive_sys)
}

func (g *gallery) gfilter(text, types string, sys_dic *dic.TrieTree) {
	texts := strings.Split(text, "")
	texts_length := len(texts)
	for i := 0; i < texts_length; i++ {
		tmpText := texts[i]
		TempMap := sys_dic
		if _, ok := TempMap.Node[tmpText]; !ok {
			continue
		}
		TempMap = TempMap.Node[tmpText]
		if TempMap.IsEnd {
			sensi := gsensitive{
				Index:    0,
				IsArr:    0,
				Key:      "",
				NewIndex: []interface{}{},
				Level:    TempMap.Level,
				Word:     []string{"", tmpText, ""},
				Type:     types,
			}
			Gsensitive = append(Gsensitive, sensi)
		}
		for j := i + 1; j < texts_length; j++ {
			jtext := texts[j]
			Tmpj, ok := TempMap.Node[jtext]
			if !ok {
				break
			} else if ok && Tmpj.IsEnd {
				tmpText += jtext
				sensi := gsensitive{
					Index:    0,
					IsArr:    0,
					Key:      "",
					NewIndex: []interface{}{},
					Level:    Tmpj.Level,
					Word:     []string{"", tmpText, ""},
					Type:     types,
				}
				Gsensitive = append(Gsensitive, sensi)
				TempMap = Tmpj
			}
		}
	}
}

func (g *gallery) gdesfilter(text, types string, index int, sys_dic *dic.TrieTree) {
	texts := strings.Split(text, "")
	texts_length := len(texts)
	for i := 0; i < texts_length; i++ {
		tmpText := texts[i]
		TempMap := sys_dic
		if _, ok := TempMap.Node[tmpText]; !ok {
			continue
		}
		TempMap = TempMap.Node[tmpText]
		if TempMap.IsEnd {
			sensi := gsensitive{
				Index:    index,
				IsArr:    1,
				Key:      "description",
				NewIndex: []interface{}{index, "description"},
				Level:    TempMap.Level,
				Word:     []string{"", tmpText, ""},
				Type:     types,
			}
			Gsensitive = append(Gsensitive, sensi)
		}
		for j := i + 1; j < texts_length; j++ {
			jtext := texts[j]
			Tmpj, ok := TempMap.Node[jtext]
			if !ok {
				break
			} else if ok && Tmpj.IsEnd {
				tmpText += jtext
				sensi := gsensitive{
					Index:    index,
					IsArr:    1,
					Key:      "description",
					NewIndex: []interface{}{index, "description"},
					Level:    Tmpj.Level,
					Word:     []string{"", tmpText, ""},
					Type:     types,
				}
				Gsensitive = append(Gsensitive, sensi)
				TempMap = Tmpj
			}
		}
	}
}
